﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using Hartley.ApplicationLogic;
using Hartley.DTOs;
using Hartley.Util;

namespace WebApplication1
{
    public partial class viewScholarshipById : System.Web.UI.Page
    {
        FundAL fundAL;
        StudentAL studentAL ;
        MemberAL memberAL;
        string queryString;
        Guid scholarshipId;
        const int SCHOLARSHIP_YR_PER_ROW = 2;
        const int SCHOLARSHIP_YR_PAGE_SIZE = 2;
        int currentPageNumber = 1;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                queryString = Request.QueryString.ToString();
                scholarshipId = Guid.Empty;

                bool convertableToGuid = Guid.TryParse(queryString, out scholarshipId);
                if (convertableToGuid)
                {

                    Session["scholarshipId"] = scholarshipId; 
                   
                    BindData(scholarshipId);

                }
                else
                {
                    //Wrong Committee Id
                }

            }
        }


        private void BindData(Guid scholarshipId)
        {

            FundAL fundAL = new FundAL();
            List<ScholarshipDTO> scholarshipDTO = new List<ScholarshipDTO>();
            List<int> allScholarshipDataYrOfOneScholarshipId = new List<int>();
            List<int> allScholarshipDataYrOfOneScholarshipIdOfOnePage = new List<int>();

            scholarshipDTO = fundAL.GetScholarShipById(scholarshipId).ToList();

            //Get the distinct years frm the acknowledged date
            List<ScholarshipDataDTO> scholDtaOfScholId = fundAL.GetAllScholarShipDataByScholarShipId(scholarshipId).ToList();
            List<int> scholDtaAllYrs = scholDtaOfScholId.OrderByDescending(b => b.amountSettledAt.Value.Year).Select(a => a.amountSettledAt.Value.Year).ToList();
            allScholarshipDataYrOfOneScholarshipId = scholDtaAllYrs.Distinct().ToList();
            // get the total rows 
            double totalRows = (int)allScholarshipDataYrOfOneScholarshipId.Count;

            lblAllScholarshipPage.Text = "Displaying all Scholarships offerered by the Colombo PPA / College - Page ";
            lblAllScholarshipOf.Text = "  of  ";

            lblAllScholarshipTotalPage.Text = CalculateTotalPages(totalRows).ToString();

            lblAllScholarshipCurrPage.Text = currentPageNumber.ToString();

            if (Convert.ToInt32(lblAllScholarshipTotalPage.Text) < 3)
            {
                if (Int32.Parse(lblAllScholarshipTotalPage.Text) <= 0)
                {
                    btnAllScholarshipFirst.Enabled = false;
                    btnAllScholarshipPrev.Enabled = false;
                    btnAllScholarshipNext.Enabled = false;
                    btnAllScholarshipLast.Enabled = false;


                }

                if (currentPageNumber == 1)
                {
                    btnAllScholarshipFirst.Enabled = false;
                    btnAllScholarshipPrev.Enabled = false;

                    if (Int32.Parse(lblAllScholarshipTotalPage.Text) <= 0)
                    {
                        btnAllScholarshipFirst.Enabled = false;
                        btnAllScholarshipPrev.Enabled = false;
                        btnAllScholarshipNext.Enabled = false;
                        btnAllScholarshipLast.Enabled = false;


                    }

                    if (Int32.Parse(lblAllScholarshipTotalPage.Text) == 1)
                    {
                        allScholarshipDataYrOfOneScholarshipIdOfOnePage = allScholarshipDataYrOfOneScholarshipId.GetRange(0, Convert.ToInt32(totalRows));

                        btnAllScholarshipNext.Enabled = false;
                        btnAllScholarshipLast.Enabled = false;


                    }
                    else if (Int32.Parse(lblAllScholarshipTotalPage.Text) == 2)
                    {
                        allScholarshipDataYrOfOneScholarshipIdOfOnePage = allScholarshipDataYrOfOneScholarshipId.GetRange(0, SCHOLARSHIP_YR_PAGE_SIZE * SCHOLARSHIP_YR_PER_ROW);

                        btnAllScholarshipNext.Enabled = true;
                        btnAllScholarshipLast.Enabled = true;

                        //lblAllMemPage.Text = "Displaying Meeting's Page(s) ";
                        //lblAllMemOf.Text = "  of  ";


                    }


                }

                else
                {

                    btnAllScholarshipPrev.Enabled = true;
                    btnAllScholarshipFirst.Enabled = true;

                    if (currentPageNumber == Int32.Parse(lblAllScholarshipTotalPage.Text))
                    {
                        allScholarshipDataYrOfOneScholarshipIdOfOnePage = allScholarshipDataYrOfOneScholarshipId.GetRange((currentPageNumber - 1) * SCHOLARSHIP_YR_PAGE_SIZE * SCHOLARSHIP_YR_PER_ROW
                            , (Convert.ToInt32(totalRows) - (currentPageNumber - 1) * SCHOLARSHIP_YR_PAGE_SIZE * SCHOLARSHIP_YR_PER_ROW));

                        btnAllScholarshipNext.Enabled = false;
                        btnAllScholarshipLast.Enabled = false;

                    }
                    else
                    {
                        allScholarshipDataYrOfOneScholarshipIdOfOnePage = allScholarshipDataYrOfOneScholarshipId.GetRange((currentPageNumber - 1) * SCHOLARSHIP_YR_PAGE_SIZE * SCHOLARSHIP_YR_PER_ROW, SCHOLARSHIP_YR_PAGE_SIZE * SCHOLARSHIP_YR_PER_ROW);

                        btnAllScholarshipNext.Enabled = true;
                        btnAllScholarshipLast.Enabled = true;


                    }

                }
            }



            else
            {
                btnAllScholarshipPrev.Enabled = true;
                btnAllScholarshipFirst.Enabled = true;

                if (currentPageNumber == Int32.Parse(lblAllScholarshipTotalPage.Text))
                {
                    allScholarshipDataYrOfOneScholarshipIdOfOnePage = allScholarshipDataYrOfOneScholarshipId.GetRange((currentPageNumber - 1) * SCHOLARSHIP_YR_PAGE_SIZE * SCHOLARSHIP_YR_PER_ROW, (Convert.ToInt32(totalRows) - (currentPageNumber - 1) * SCHOLARSHIP_YR_PAGE_SIZE * SCHOLARSHIP_YR_PER_ROW));

                    btnAllScholarshipNext.Enabled = false;
                    btnAllScholarshipLast.Enabled = false;

                    //lblAllMemPage.Text = "Displaying Meeting's Page(s) ";
                    //lblAllMemOf.Text = "  of  ";
                }
                else
                {
                    allScholarshipDataYrOfOneScholarshipIdOfOnePage = allScholarshipDataYrOfOneScholarshipId.GetRange((currentPageNumber - 1) * SCHOLARSHIP_YR_PAGE_SIZE * SCHOLARSHIP_YR_PER_ROW, SCHOLARSHIP_YR_PAGE_SIZE * SCHOLARSHIP_YR_PER_ROW);

                    btnAllScholarshipNext.Enabled = true;
                    btnAllScholarshipLast.Enabled = true;

                    //lblAllMemPage.Text = "Displaying Meeting's Page(s) ";
                    //lblAllMemOf.Text = "  of  ";
                }


            }


            CreateDynamicTable(allScholarshipDataYrOfOneScholarshipIdOfOnePage, scholarshipId);
           


        }

        private int CalculateTotalPages(double totalRows)
        {
            int totalPages = (int)Math.Ceiling(totalRows / (SCHOLARSHIP_YR_PAGE_SIZE * SCHOLARSHIP_YR_PER_ROW));

            return totalPages;
        }

        protected void ChangePage(object sender, CommandEventArgs e)
        {

            switch (e.CommandName)
            {

                case "First":
                    currentPageNumber = 1;
                    break;

                case "Previous":
                    currentPageNumber = Int32.Parse(lblAllScholarshipCurrPage.Text) - 1;
                    break;

                case "Next":
                    currentPageNumber = Int32.Parse(lblAllScholarshipCurrPage.Text) + 1;
                    break;
                case "Last":
                    currentPageNumber = Int32.Parse(lblAllScholarshipTotalPage.Text);
                    break;
            }


            BindData( new Guid(Session["scholarshipId"].ToString()));
        }

        private void CreateDynamicTable(List<int> allScholarshipDataYrOfOneScholarshipIdOfOnePage, Guid scholarshipId)
        {
            PlaceHolder6.Controls.Clear();


            fundAL = new FundAL();
            studentAL = new StudentAL();
            memberAL = new MemberAL();
            SportsUtil sportsUtil = new SportsUtil();
            List<ScholarshipDTO> scholarshipDTO = fundAL.GetScholarShipById(scholarshipId);
            //List<ScholarshipDataDTO> scholarshipDataDTO = fundAL.GetAllScholarShipDataByScholarShipId(scholarshipId).OrderBy(a => a.amountSettledAt).ToList();

            // Create a Table and set its properties 
            Table tbl = new Table();
            tbl.Width = new Unit("100%");
            tbl.BorderStyle = BorderStyle.Solid;
            // Add the table to the placeholder control
            PlaceHolder6.Controls.Add(tbl);


            Table tblYrHead = new Table();
            tblYrHead.Width = new Unit("100%");
            tblYrHead.BorderStyle = BorderStyle.Solid;
            PlaceHolder6.Controls.Add(tblYrHead);

            Table tblYrBody = new Table();
            tblYrBody.Width = new Unit("100%");
            tblYrBody.BorderStyle = BorderStyle.Solid;
            PlaceHolder6.Controls.Add(tblYrBody);

            Label lblScholarshipName = new Label();
            lblScholarshipName.Text = "Scholarship Name";

            Label lblScholarshipInMomorial = new Label();
            lblScholarshipInMomorial.Text = "In Memorial Of";

            Label lblScholarshipCondition = new Label();
            lblScholarshipCondition.Text = "Condition";

            Label lblScholarshipOfferedBy = new Label();
            lblScholarshipOfferedBy.Text = "OfferedBy";

            Label lblScholarshipCapital = new Label();
            lblScholarshipCapital.Text = "Capital";

            Label lblScholarshipAMS = new Label();
            lblScholarshipAMS.Text = "Amount Per Slot";

            Label lblScholarshipDesc = new Label();
            lblScholarshipDesc.Text = "Description";


            List<Label> allHeaderLabels = new List<Label>();

            allHeaderLabels.Add(lblScholarshipName);
            allHeaderLabels.Add(lblScholarshipInMomorial);
            allHeaderLabels.Add(lblScholarshipCondition);
            allHeaderLabels.Add(lblScholarshipOfferedBy);
            allHeaderLabels.Add(lblScholarshipCapital);
            allHeaderLabels.Add(lblScholarshipAMS);
            allHeaderLabels.Add(lblScholarshipDesc);


            int i = 0;
            foreach (Label lblHeader in allHeaderLabels)
            {


                TableRow trHeader = new TableRow();
                trHeader.Width = new Unit("50%");

                TableCell tcHeader = new TableCell();
                TableCell tcHeaderValue = new TableCell();

                HyperLink lblValue = new HyperLink();
                if (i % 2 == 0)
                {
                    tcHeader.BackColor = System.Drawing.Color.Blue;
                    tcHeader.ForeColor = System.Drawing.Color.White;

                    tcHeaderValue.BackColor = System.Drawing.Color.Blue;
                    tcHeaderValue.ForeColor = System.Drawing.Color.White;

                }
                else
                {
                    tcHeader.BackColor = System.Drawing.Color.White;
                    tcHeader.ForeColor = System.Drawing.Color.Blue;

                    tcHeaderValue.BackColor = System.Drawing.Color.White;
                    tcHeaderValue.ForeColor = System.Drawing.Color.Blue;
                }

                if (lblHeader.Text == "Scholarship Name")
                {
                    lblValue.Text = scholarshipDTO[0].Title;
                }

                else if (lblHeader.Text == "In Memorial Of")
                {
                    lblValue.Text = scholarshipDTO[0].InMemorialOf;
                }

                else if (lblHeader.Text == "Condition")
                {
                    lblValue.Text = scholarshipDTO[0].Condition;
                }

                else if (lblHeader.Text == "OfferedBy")
                {
                    //Guid offeredById = scholarshipDTO[0].OfferedBy;
                    lblValue.Text = memberAL.GetMemberById(scholarshipDTO[0].OfferedBy.Value).SingleOrDefault().FullName;
                    lblValue.NavigateUrl = "viewMember.aspx?" + memberAL.GetMemberById(scholarshipDTO[0].OfferedBy.Value).SingleOrDefault().MemberId;
                }
                else if (lblHeader.Text == "Capital")
                {
                    lblValue.Text = scholarshipDTO[0].Capital.ToString();
                }
                else if (lblHeader.Text == "Amount Per Slot")
                {
                    lblValue.Text = scholarshipDTO[0].Value.ToString();
                }
                else if (lblHeader.Text == "Description")
                {
                    lblValue.Text = scholarshipDTO[0].Description;
                }
                /* else if (lblHeader.Text == "Contribution")
                {
                    lblValue.Text = committeeDTO[0].Contribution;
                }*/
                tcHeader.Controls.Add(lblHeader);
                tcHeaderValue.Controls.Add(lblValue);
                trHeader.Cells.Add(tcHeader);
                trHeader.Cells.Add(tcHeaderValue);


                tbl.Rows.Add(trHeader);
                i++;
            }

            /*TableRow trScholYrHeader = new TableRow();
            trScholYrHeader.Width = new Unit("100%");

            TableCell tcScholYrHeader = new TableCell();
            //TableCell tcScholYrHeader1 = new TableCell();

            
            Label lblScholYrHeader = new Label();
            if(i % 2 == 0)
            {
                tcScholYrHeader.BackColor = System.Drawing.Color.Blue;
                tcScholYrHeader.ForeColor = System.Drawing.Color.White;
            }
            else
            {
                tcScholYrHeader.BackColor = System.Drawing.Color.White;
                tcScholYrHeader.ForeColor = System.Drawing.Color.Blue;
            }
            lblScholYrHeader.Text = "Year wise of this scholarship";

            tcScholYrHeader.HorizontalAlign = HorizontalAlign.Center;
            tcScholYrHeader.Controls.Add(lblScholYrHeader);
            trScholYrHeader.Cells.Add(tcScholYrHeader);
            tbl.Rows.Add(trScholYrHeader);*/


            int tblRows = allScholarshipDataYrOfOneScholarshipIdOfOnePage.Count / SCHOLARSHIP_YR_PER_ROW;
            int tblCols = 0;

            for (int j = 0; j <= tblRows; j++)
            {
                TableRow trScholYrValue = new TableRow();
                trScholYrValue.Width = new Unit("25%");



                if (j != tblRows && tblRows > 1)
                {
                    tblCols = SCHOLARSHIP_YR_PER_ROW;
                }
                else
                {
                    tblCols = allScholarshipDataYrOfOneScholarshipIdOfOnePage.Count % SCHOLARSHIP_YR_PER_ROW;
                }


                for (int k = 0; k < tblCols; k++)
                {
                    TableCell tcScholYrValue = new TableCell();

                    //HyperLink hlAthleticYrCell = new HyperLink();
                    HyperLink hlScholYrValue = new HyperLink();

                    hlScholYrValue.Text = allScholarshipDataYrOfOneScholarshipIdOfOnePage[j * SCHOLARSHIP_YR_PER_ROW + k].ToString();
                    hlScholYrValue.NavigateUrl = "viewOneScholarshipInYrWise.aspx?" +"SelectedScholarshipId="+ scholarshipId +"&SelectedYear="+ sportsUtil.encryptQueryStrings(allScholarshipDataYrOfOneScholarshipIdOfOnePage[j * SCHOLARSHIP_YR_PER_ROW + k].ToString());

                    //hlScholYrValue.NavigateUrl = "viewAthleticDataAsGrid.aspx?" + allScholarshipDataYrOfOneScholarshipIdOfOnePage[j * SCHOLARSHIP_YR_PER_ROW + k].ToString();

                    if ((i +j + 1) % 2 == 0)
                    {
                        tcScholYrValue.BackColor = System.Drawing.Color.Blue;
                        tcScholYrValue.ForeColor = System.Drawing.Color.White;

                    }
                    else
                    {
                        tcScholYrValue.BackColor = System.Drawing.Color.White;
                        tcScholYrValue.ForeColor = System.Drawing.Color.Blue;


                    }

                    //hlScholYrValue.Text = allScholarshipDataYrOfOneScholarshipIdOfOnePage[k].ToString();

                    tcScholYrValue.Controls.Add(hlScholYrValue);
                    //tcBodyMemberName.Controls.Add(hlMemberName);
                    //}


                    trScholYrValue.Cells.Add(tcScholYrValue);
                    //trScholYr.Cells.Add(tcBodyMemberName);


                }

                tblYrBody.Rows.Add(trScholYrValue);
            }



          


            //add heading
            TableRow trScholYr = new TableRow();
            trScholYr.Width = new Unit("50%");

            TableCell tcScholYr = new TableCell();
            //TableCell tcBodyMemberName = new TableCell();

            Label lblScholYr = new Label();
            // HyperLink hlMemberName = new HyperLink();
            if (i % 2 == 0)
            {
                tcScholYr.BackColor = System.Drawing.Color.DarkSlateBlue;
                tcScholYr.ForeColor = System.Drawing.Color.White;

            }
            else
            {
                tcScholYr.BackColor = System.Drawing.Color.White;
                tcScholYr.ForeColor = System.Drawing.Color.DarkSlateBlue;

            }

            lblScholYr.Text = "Year wise of this scholarship";

            tcScholYr.HorizontalAlign = HorizontalAlign.Center;
            //lblScholYr.Text = allScholarshipDataYrOfOneScholarshipIdOfOnePage[j].ToString();

            tcScholYr.Controls.Add(lblScholYr);
            //tcBodyMemberName.Controls.Add(hlMemberName);
            //}

            trScholYr.Cells.Add(tcScholYr);
            //trScholYr.Cells.Add(tcBodyMemberName);



            tblYrHead.Rows.Add(trScholYr);

            ViewState["dynamictable"] = true;

            i++;

        }
        
    }
}